package com.git.mark.dao;

import git.bankview.sl.db.DBConnectionFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import javax.naming.NamingException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;

public class MarkBaseDao {
	
	public static ResultSet rs = null;  
    public static Connection conn = null;  
    public static PreparedStatement pstmt = null;  
//    private Log logger = LogFactory.getLog(getClass());  
	private static Logger log = Logger.getLogger(MarkDaoImpl.class);
//	private static DataSource m_dataSource;
    
	public  Connection getCogdbConnection(){
		/*	if (null == m_dataSource)
			{
				Context ctx = new InitialContext();
				m_dataSource = (DataSource)ctx.lookup("java:comp/env/jdbc/COGDB");
			}
			return m_dataSource.getConnection();*/
//			try {
//				Class.forName("oracle.jdbc.driver.OracleDriver");
//				conn = java.sql.DriverManager.getConnection(
//						"jdbc:oracle:thin:@172.17.22.48:1521:cogdb", "ods", "ods"); 
//	        } catch (Exception e) {  
//	        	e.printStackTrace();
//	        }  
//		
//			return conn;
			try {
				conn = DBConnectionFactory.getCoreConnection();
//				log.info("open database success");  
			} catch (NamingException e) {
				e.printStackTrace();
				log.info(e.getMessage());
			} catch (SQLException e) {
				e.printStackTrace();
				log.info(e.getMessage());
			}
			
			return conn;
			
		}
    
    public void close() {  
        try {  
            if (rs != null) {  
                rs.close();  
            }  
            if (conn != null) {  
                conn.close();  
            }  
            if (pstmt != null) {  
                pstmt.close();  
            }  
//            log.info("Close database success");  
        } catch (Exception e) {  
        	log.error("Close database error " + e.toString());  
        }  
    }  
    
    public static String getSequence(){
    	MarkBaseDao mb = new MarkBaseDao();
    	
    	conn = mb.getCogdbConnection();
    	String sql = "select SEQ_REPORTMODEL.Nextval from dual";
    	String number = "";
    	try {
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while(rs.next()){
				number = rs.getString(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			mb.close();
		}
    	return number;
    } 
}
